package clone;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Student3 implements Cloneable, Serializable {
	private String name;
	private int age;
	private Teacher teacher;

	public Student3(String name, int age, Teacher teacher) {
		this.name = name;
		this.age = age;
		this.teacher = teacher;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	public Student3 clone() {
		Student3 student = null;
		try {
			// 对象流
//			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("d:/temp/dd.dat")));
//			oos.writeObject(this);
//			ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("d:/temp/dd.dat")));
//			student = (Student3) ois.readObject();
			
			// bytearray输出流对象，用于临时缓存输出流内容
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			// 对象输出流，将对象输出流输出到字节数组的输出流对象中
			ObjectOutputStream oos2 = new ObjectOutputStream(bos);
			oos2.writeObject(this); // 写对象
			// 使用对象输入流从字节数组输出流中将字节流内容读取进来
			ObjectInputStream ois2 = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
			student = (Student3) ois2.readObject();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return student;
	}

	@Override
	public String toString() {
		return "Student3 [name=" + name + ", age=" + age + ", teacher=" + teacher + "]";
	}

}
